﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECommerce.Model.Member;
using Jzy.Core.Common;
using ECommerce.DAL.Member;
using ECommerce.Common;
using Jzy.Core.Utility;

namespace ECommerce.BLL.Member
{
    public class MemberBaseBO : CommonOperate<MemberBaseInfo>
    {
        private MemberBaseDAL _memberBase;

        public MemberBaseBO()
        {
            _memberBase = new MemberBaseDAL();
        }

        public void Startup(List<int> lstMemberIDs)
        {
            _memberBase.Startup(lstMemberIDs);
        }

        public void Stop(List<int> lstMemberIDs)
        {
            _memberBase.Stop(lstMemberIDs);
        }

        public MemberBaseInfo GetInfoByLoginName(string loginName)
        {
            return _memberBase.GetInfoByLoginName(loginName);
        }

        public void SetSession(string strLoginName)
        {
            MemberBaseInfo info = this.GetInfoByLoginName(strLoginName);
            SiteSession.MemberID = info.ID;
            SiteSession.MemberLoginName = info.LoginName;
           

            SiteCookie.MemberLoginName = info.LoginName;
            SiteCookie.Password = info.Password;//密码是已经加过密的
        }

        public string Encript(MemberBaseInfo info)
        {
            return StrUtil.EncryptDES(info.Password, info.LoginName);

        }

        public double GetRemain(int memberID)
        {
            return _memberBase.GetRemain(memberID);
        }

        public void UpdateRemain(double remain, int memberID)
        {
            _memberBase.UpdateRemain(remain, memberID);
        }
        public string Decript(MemberBaseInfo info)
        {
            try
            {
                return StrUtil.DecryptDES(info.Password, info.LoginName);
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strLoginName"></param>
        /// <param name="strPassword">非加密的密码</param>
        /// <returns></returns>
        public LoginResult CheckLogin(string strLoginName, string strPassword)
        {
            LoginResult result = LoginResult.Success;

            MemberBaseInfo info = this.GetInfoByLoginName(strLoginName);
            if (info == null)
            {                
                result = LoginResult.UserErr;               
            }
            else
            {
                if (info.State == (int)MemberState.启用)
                {
                    if (this.Decript(info) ==  strPassword)
                    {
                        result = LoginResult.Success;
                    }
                    else
                    {
                        result = LoginResult.PwdErr;
                    }
                }
                else
                {
                    result = LoginResult.Forbidden;
                }

            }
            return result;
        }

        public void ClearSession()
        {

            SiteSession.Clear();

            SiteCookie.Clear();
        }
    }
}
